package com.jeesite.modules.task.feedback.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.task.deploy.entity.DcSupTaskInfo;
import com.jeesite.modules.task.deploy.service.DcSupTaskInfoService;
import com.jeesite.modules.task.feedback.service.DcSupTaskSendFeedbackMsgService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.feedback.entity.DcSupTaskFeedback;
import com.jeesite.modules.task.feedback.service.DcSupTaskFeedbackService;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 任务反馈信息表Controller
 * @author zrr
 * @version 2022-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/feedback/dcSupTaskFeedback")
public class DcSupTaskFeedbackController extends BaseController {

	@Autowired
	private DcSupTaskFeedbackService dcSupTaskFeedbackService;
	@Autowired
	private DcSupTaskInfoService dcSupTaskInfoService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskFeedback get(String id, boolean isNewRecord) {
		return dcSupTaskFeedbackService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskFeedback dcSupTaskFeedback, Model model) {
		model.addAttribute("dcSupTaskFeedback", dcSupTaskFeedback);
		return "modules/feedback/dcSupTaskFeedbackList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskFeedback> listData(DcSupTaskFeedback dcSupTaskFeedback, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskFeedback.setPage(new Page<>(request, response));
		Page<DcSupTaskFeedback> page = dcSupTaskFeedbackService.findPage(dcSupTaskFeedback);
		for (int i = 0; i < page.getList().size(); i++) {
			page.getList().get(i).setFeedbackEndTimeStr(DateUtils.formatDate(page.getList().get(i).getFeedbackEndTime(),"yyyy-MM-dd"));
			page.getList().get(i).setFeedbackTimeStr(DateUtils.formatDate(page.getList().get(i).getFeedbackTime(),"yyyy-MM-dd"));
		}
		return page;
	}

	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "listDataOverRide")
	@ResponseBody
	public Page<DcSupTaskFeedback> listDataOverRide(DcSupTaskFeedback dcSupTaskFeedback, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskFeedback.setPage(new Page<>(request, response));
		Page<DcSupTaskFeedback> page = dcSupTaskFeedbackService.listDataOverRide(dcSupTaskFeedback);
		return page;
	}

	@RequestMapping(value = "queryfeedbackHistory")
	@ResponseBody
	public Page<DcSupTaskFeedback> queryfeedbackHistory(DcSupTaskFeedback dcSupTaskFeedback, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskFeedback.setPage(new Page<>(request, response));
		Page<DcSupTaskFeedback> page = dcSupTaskFeedbackService.queryfeedbackHistory(dcSupTaskFeedback);
		return page;
	}

	/**
	 * 查询反馈列表
	 * @param dcSupTaskFeedback
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequiresPermissions("audit:taskReceive:view")
	@RequestMapping(value = "feedbacklistData")
	@ResponseBody
	public Page<DcSupTaskFeedback> feedbacklistData(DcSupTaskFeedback dcSupTaskFeedback, HttpServletRequest request, HttpServletResponse response) {
		dcSupTaskFeedback.setPage(new Page<>(request, response));
		DcSupTaskInfo dcSupTaskInfo = dcSupTaskInfoService.get(dcSupTaskFeedback.getTaskId());
		String typeCode=null;
		if(dcSupTaskInfo!=null){
			typeCode =dcSupTaskInfo.getTaskTypeCode();
		}

		Page<DcSupTaskFeedback> page = dcSupTaskFeedbackService.feedbacklistData(dcSupTaskFeedback);
		String finalTypeCode = typeCode;
		page.getList().forEach(x->x.setTaskTypeCode(finalTypeCode));
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:view")
	@RequestMapping(value = "form")
	public String form(DcSupTaskFeedback dcSupTaskFeedback, Model model) {
		model.addAttribute("dcSupTaskFeedback", dcSupTaskFeedback);
		return "modules/feedback/dcSupTaskFeedbackForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupTaskFeedback dcSupTaskFeedback) {
		dcSupTaskFeedbackService.save(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("保存任务反馈信息成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupTaskFeedback dcSupTaskFeedback) {
		dcSupTaskFeedback.setStatus(DcSupTaskFeedback.STATUS_DISABLE);
		dcSupTaskFeedbackService.updateStatus(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("停用任务反馈信息成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupTaskFeedback dcSupTaskFeedback) {
		dcSupTaskFeedback.setStatus(DcSupTaskFeedback.STATUS_NORMAL);
		dcSupTaskFeedbackService.updateStatus(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("启用任务反馈信息成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("feedback:dcSupTaskFeedback:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskFeedback dcSupTaskFeedback) {
		dcSupTaskFeedbackService.delete(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("删除任务反馈信息成功！"));
	}

	@RequestMapping(value = "getOverRide")
	@ResponseBody
	public String getOverRide(DcSupTaskFeedback dcSupTaskFeedback) {
		DcSupTaskFeedback overRide = dcSupTaskFeedbackService.getOverRide(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("查询成功！"),overRide==null?"":overRide);
	}

	@RequestMapping(value = "isFirstOrLast")
	@ResponseBody
	public String isFirstOrLast(String actionId) {
		Integer isFirstOrLast = dcSupTaskFeedbackService.isFirstOrLast(actionId);
		return renderResult(Global.TRUE, text("查询成功！"),isFirstOrLast);
	}

	@RequestMapping(value = {"addFeedback", ""})
	public String addFeedback(DcSupTaskFeedback dcSupTaskFeedback, Model model) {
		DcSupTaskFeedback newFeedbackByActionId = dcSupTaskFeedbackService.getNewFeedbackByActionId(dcSupTaskFeedback.getActionId());
		if (!StringUtils.equals(newFeedbackByActionId.getFeedbackStatus(),"11")){
			newFeedbackByActionId.setFeedbackStatus("-1");
		}
		newFeedbackByActionId.setFeedbackEndTime(null);
		model.addAttribute("dcSupTaskFeedback", newFeedbackByActionId);
		return "modules/task/feedback/addFeedback";
	}

	@RequestMapping(value = "addFeedbackFunc")
	@ResponseBody
	public String addFeedbackFunc(DcSupTaskFeedback dcSupTaskFeedback) {
		dcSupTaskFeedbackService.addFeedbackFunc(dcSupTaskFeedback);
		return renderResult(Global.TRUE, text("新增成功！"));
	}

//	@RequestMapping(value = "sendYzwFeedbackMessageNew")
//	@ResponseBody
//	public String sendYzwFeedbackMessageNew() {
//		dcSupTaskSendFeedbackMsgService.sendYzwFeedbackMessageNew();
//		return renderResult(Global.TRUE, text("成功！"));
//	}
	
}